package com.miluvs.service;
import io.milvus.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class MiluvsService {

    @Autowired
    private MilvusClient milvusClient;
    final String SEARCH_PARAM = "{\"nprobe\":16}";

  public static   final int dimension = 100;

    public String createCollection(String collectionName ) {
        CollectionMapping mapping = new CollectionMapping
                .Builder(collectionName, dimension)
                .withIndexFileSize(1314)
                .withMetricType(MetricType.IP)
                .build();
        milvusClient.createCollection(mapping);
        return "ok";
    }

    public String createCollectTitle(String collectionName) {
        milvusClient.createPartition(collectionName, "dam");
        return "ok";
    }

    public boolean hasCollection(String collectionName) {
        milvusClient.hasCollection(collectionName);
        return true;
    }

    public String addVectors(String collectionName) {
        List<Long> list =new ArrayList<>();
        List<List<Float>> floatVectors = new ArrayList<>();
        getList( list,  floatVectors);
        InsertParam insertParam = new InsertParam.Builder(collectionName)
                .withFloatVectors(floatVectors)
                .withPartitionTag("dam")
                .withVectorIds(list)
                .build();
        milvusClient.insert(insertParam);

        return "ok";
    }

    public SearchResponse searchVectors(String collectionName,String search) {
        List<String> partitionTags = new ArrayList<>();
        partitionTags.add("dam");
        List<List<Float>> floatVectors = new ArrayList<>();
        floatVectors.add(generateVector(search,dimension));
        SearchParam searchParam = new SearchParam.Builder(collectionName)
                .withFloatVectors(floatVectors)
                .withTopK(1)
                .withParamsInJson(SEARCH_PARAM)
                .withPartitionTags(partitionTags)
                .build();
        return milvusClient.search(searchParam);
    }

    public static  void getList(List<Long> list, List<List<Float>> floatVectors) {
        floatVectors.add(generateVector("你好呀",dimension));
        floatVectors.add(generateVector("测试拉拉",dimension));
        floatVectors.add(generateVector("标准",dimension));
        floatVectors.add(generateVector("无线",dimension));
        floatVectors.add(generateVector("表明",dimension));
        list.add(1L);
        list.add(2L);
        list.add(3L);
        list.add(4L);
        list.add(5L);
    }

    public static List<Float> generateVector(String inputString, int dimension) {
        // 在这里实现你的逻辑，使用模型将输入字符串转换为向量数组
        // 你可以使用 wordVectors 的方法来计算输入字符串的向量表示

        // 示例代码，将输入字符串的每个字符转换为 Unicode 编码值作为向量分量
        Float[] vectorArray = new Float[dimension];
        double[] vectorData = new double[dimension];
        for (char c : inputString.toCharArray()) {
            vectorData[c % dimension] += (double) c;
        }
        if (vectorData.length > 0) {
            for (int i = 0; i < vectorData.length; i++) {
                vectorArray[i] = (float) vectorData[i];
            }
        }
        return (List<Float>) Arrays.asList(vectorArray);
    }


}

